package com.edu.hre.struts.action.dashboard;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.bsf.util.Bean;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.LabelValueBean;

import com.edu.hre.common.entity.BeanChart;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;

public class DashResourceAction extends DispatchAction {
	
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation()); 
		executeStatic(request);
		return mapping.findForward("dashResource");
	}
	
	public static void executeStatic(HttpServletRequest request){
		
		int countGenderMale =   baseService.countByColumns(new String[]{"empGender", "isDeleted"}, new int[]{Compare.EQUAL, Compare.EQUAL}, new Object[]{new Short("2"), false}, "empNumber", HreEmployee.class);  
		int countGenderFemale = baseService.countByColumns(new String[]{"empGender", "isDeleted"}, new int[]{Compare.EQUAL, Compare.EQUAL}, new Object[]{new Short("1"), false}, "empNumber", HreEmployee.class);
		int countGenderUnknown = baseService.countByColumns(new String[]{"empGender", "empGender", "isDeleted"}, new int[]{Compare.NOT_EQUAL, Compare.NOT_EQUAL, Compare.EQUAL} , new Object[]{new Short("2"), new Short("1"), false}, "empNumber", HreEmployee.class);
		if(countGenderMale + countGenderFemale + countGenderUnknown != 0){
			request.setAttribute("pie_partsNumber", countGenderMale + ";" + countGenderFemale + ";" + countGenderUnknown);
			request.setAttribute("pie_partsName", "Male;Female;Unknown");
			request.setAttribute("pie_topic", "Grouped by Gender");
			request.setAttribute("pie_totalLeaveDays", (Float.valueOf(countGenderMale + countGenderFemale + countGenderUnknown)));
			
			CommonUtils.println("pie_partsNumber: " + countGenderMale + ";" + countGenderFemale + ";" + countGenderUnknown);
			CommonUtils.println("pie_partsName: " + "Male;Female;Unknown");
			CommonUtils.println("pie_totalLeaveDays: " + (countGenderMale + countGenderFemale + countGenderUnknown));
		}
		// ================================================================================================================================================
		
		List<HreEmployee> empList = baseService.getPojos("isDeleted", Compare.EQUAL, false, null, null, 0, 0, HreEmployee.class);
		int age00 = countEmp(empList, 0, 0);
		int age020 = countEmp(empList, 0, 20);
		int age2025 = countEmp(empList, 20, 25);
		int age2530 = countEmp(empList, 25, 30);
		int age3035 = countEmp(empList, 30, 35);
		int age3540 = countEmp(empList, 35, 40);
		int age4050 = countEmp(empList, 40, 50);
		int age5010000 = countEmp(empList, 50, 10000);

		String pie2_partsNames = "Unknown;< 20;>= 20 and < 25;>= 25 and < 30;>= 30 and < 35;>= 35 and < 40;>= 40 and < 50;>= 50";
		String pie2_partsNumbers = age00 + ";" + age020 + ";" + age2025 + ";" + age2530 + ";" + age3035 + ";" + age3540 + ";" + age4050 + ";" + age5010000;
		int pie2_totalLeaveDays = age00 + age020 + age2025 + age2530 + age3035 + age3540 + age4050 + age5010000;
		if(pie2_totalLeaveDays != 0){
			request.setAttribute("pie2_partsName", pie2_partsNames);
			request.setAttribute("pie2_partsNumber", pie2_partsNumbers);
			request.setAttribute("pie2_totalLeaveDays", Float.valueOf(pie2_totalLeaveDays));
			request.setAttribute("pie2_topic", "Grouped by Age");
			
			CommonUtils.println("pie2_partsName: " + pie2_partsNames);
			CommonUtils.println("pie2_partsNumber: " + pie2_partsNumbers);
			CommonUtils.println("pie2_totalLeaveDays: " + pie2_totalLeaveDays);
		}
		// ================================================================================================================================================
		
		
		
		String bar_partsName = "";
		String bar_partsNumber = "";
		List<LabelValueBean> listBean = getListBean();
		for(int i = 0; listBean != null && i < listBean.size(); i++){
			LabelValueBean bean = listBean.get(i);
			bar_partsName += ";" + bean.getLabel();
			bar_partsNumber += ";" + bean.getValue();
		}
		bar_partsName = bar_partsName.replaceFirst(";", "");
		bar_partsNumber = bar_partsNumber.replaceFirst(";", "");
		request.setAttribute("bar_partsName", bar_partsName);
		request.setAttribute("bar_partsNumber", bar_partsNumber);
		request.setAttribute("bar_topic", "Structure Distribution");
		CommonUtils.println("bar_partsName: " + bar_partsName);
		CommonUtils.println("bar_partsNumber: " + bar_partsNumber);
	}
	
	
	private static Integer computeAge(Date birthfay){
		if(birthfay != null){ 
			return new Date().getYear() + 1900 - birthfay.getYear();
		}
		return -1;
	}
	
	private static int countEmp(List<HreEmployee> empList, int greater_equal, int less){
		if(empList == null || empList.size() == 0){
			return  0;
		}
		int count = 0;
		for(int i = 0; empList != null && i < empList.size(); i++){
			HreEmployee tmp = empList.get(i);
			if(greater_equal == 0 && less == 0){
				if(tmp.getEmpBirthday() == null){
					count ++;
				}
			} else {
				int age = computeAge(tmp.getEmpBirthday());
				if(age >= greater_equal && age < less){
					count ++;
				}
			}
		}
		return count;
	}
	
	private static List<LabelValueBean> getListBean(){
		String sql = "select  count(*) count, t.id, t.name " + 
					"from " +
					    "( " +
					        "SELECT s.id, s.name " +
					        "FROM hrentry_db.hre_employee e " + 
					        "left outer join hre_subunit s " +
					        "on s.id = e.work_station " +
					        "where e.is_deleted = false " +
					    ") as t " +
					"group by t.id, t.name"; 
		List<Object[]> listResult = baseService.listArrayObject(sql, 0, 0);
		List<LabelValueBean> listTmp = new ArrayList<LabelValueBean>();
		for(int i = 0; listResult != null && i < listResult.size(); i++){
			Object[] objs = listResult.get(i);
			String name = (String)objs[3] == null ? "Unknown" : (String)objs[3];
			listTmp.add(new LabelValueBean(name, ""+ ((BigInteger)objs[1]).intValue()));
		}
		return listTmp;
	}
}
